package com.ruoyi.androidPad.controller;

import com.alibaba.fastjson.JSON;
import com.ruoyi.androidPad.domain.OperationSet;
import com.ruoyi.androidPad.mqtt.config.MqttPushClient;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author LiSuxuan
 * @date 2021年 08月 18日 16:48
 */
@Api("mqtt推送部分")
@Slf4j
@RestController
@RequestMapping("/mqtt")
public class MqttController extends BaseController {

    @Autowired
    private MqttPushClient mqttPushClient;


    @PostMapping("/test")
    public AjaxResult testPublic() {
        boolean isSuccess = mqttPushClient.publish(1, false, "server", "测试：java---> MQTT");
        if (isSuccess) {
            return AjaxResult.success();
        }
        return AjaxResult.error("消息发布失败");
    }

    /**
     * 服务端向安卓平板发送指令
     *
     * @param operationSet  对象信息
     * @return
     */
    @ApiOperation(value = "服务端向安卓平板发送指令", notes = "http://localhost:8180/mqtt/padSet" + "\n body传参:" +"{\n" +
            "      \"shootDelay\": 1,\n" +
            "      \"takePhoto\": false,\n" +
            "      \"checkSet\": true\n" +
            "}")
    @PostMapping("/padSet")
    public AjaxResult padPublic(@RequestBody OperationSet operationSet) {
        String operationSetString = JSON.toJSON(operationSet).toString();
        boolean isSuccess = mqttPushClient.publish(1, false, "server/pad/set", operationSetString);
        if (isSuccess) {
            return AjaxResult.success();
        }
        return AjaxResult.error("消息发布失败");
    }


}
